from .utils import *
import re
# 获取电影年份
def getYearData():
    timeList = list(df['time'].map(lambda x: int(x[:4])))
    timeList.sort()
    timeObj = {}
    for i in timeList:
        if timeObj.get(i, -1) == -1:
            timeObj[i] = 1
        else:
            timeObj[i] = timeObj[i] + 1
    return list(timeObj.keys()), list(timeObj.values())

# 获取电影时长
def getMovieTimeData():
    # 因为movieTime中含有数字以外的字符，故用正则表达式进行处理，只提取出数字并转换为int类型，用于计算时长
    movieTimeList = list(df['movieTime'].map(lambda x: int(re.search('\d+', x).group())))
    movieTimeData = [
        {
            'name': '短',
            'value': 0
        },
        {
            'name': '中',
            'value': 0
        },
        {
            'name': '长',
            'value': 0
        },
        {
            'name': '特长',
            'value': 0
        },
    ]
    for i in movieTimeList:
        if i <= 90:
            movieTimeData[0]['value'] = movieTimeData[0]['value'] + 1
        elif i <= 110:
            movieTimeData[1]['value'] = movieTimeData[1]['value'] + 1
        elif i <= 130:
            movieTimeData[2]['value'] = movieTimeData[2]['value'] + 1
        else:
            movieTimeData[3]['value'] = movieTimeData[3]['value'] + 1
    return movieTimeData

# test -- from utils import *
# print(getYearData())
# print(getMovieTimeData())